<?php

include "../lib/start.php";

check_session();
check_permission(ADM_PERM);

header("Content-type: text/plain");

$sel_classi = "SELECT id_classe FROM classi ORDER BY id_classe";
try{
	$res_classi = $db->executeQuery($sel_classi);
} catch (MySQLException $ex){
	print "ko;".$ex->getQuery().";".$ex->getMessage();
	exit;
}

$sel_anno = "SELECT * FROM anni ORDER BY id_anno DESC LIMIT 1";
$res_anno = $db->executeQuery($sel_anno);
$year = new AnnoScolastico($res_anno->fetch_assoc());
$anno = $year->get_ID();

while($classe = $res_classi->fetch_assoc()){
    $sel_materie = "SELECT id_materia FROM materie WHERE id_materia != 999 AND (id_materia < 12 OR id_materia > 20) ORDER BY id_materia";
    $res_materie = $db->executeQuery($sel_materie);
    while($mat = $res_materie->fetch_assoc()){
        $ins = "INSERT INTO cdc (id_anno, id_classe, id_docente, id_materia) VALUES ($anno, ".$classe['id_classe'].", NULL, ".$mat['id_materia'].")";
        try{
        	$rs = $db->executeUpdate($ins);
        } catch (MySQLException $ex){
        	print "ko;".$ex->getQuery().";".$ex->getMessage();
			exit;
        }
    }
}

print "ok;";
exit;

?>